
//
//  PermissionsViewController.m
//  TaiHao
//
//  Created by rzm on 16/6/3.
//  Copyright © 2016年 rzm. All rights reserved.
//

#import "SampuxInstallPermissionsVC.h"
#import "SampuxPermissionsView.h"
#import "SampuxAddUserView.h"
#import "SampuxAuthorityTransferView.h"
#import "SampuxRecordViewController.h"
#import "SampuxBubbleView.h"

@interface SampuxInstallPermissionsVC ()<AddUserViewYesBtnDelegate,SampuxHailFellowSelectedRowDelegate>
{
    NSIndexPath *_indexPath;
}
@property (nonatomic, strong) UIButton *authorityTransferBtn;
@property (nonatomic, strong) UIButton *addUserBtn;
@property (nonatomic, strong) SampuxAuthorityTransferView *transferView;
@property (nonatomic, strong) SampuxAddUserView *addUserView;
@property (nonatomic, strong) SampuxPermissionsView *permissionsView;
@property (nonatomic, strong) SampuxBubbleView *bubbleView;

//@property (nonatomic, strong)  UIImageView *bubbleView;
@property (nonatomic, assign) BOOL needRefresh;

@end

@implementation SampuxInstallPermissionsVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem setHidesBackButton:YES];

//    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(refreshData) name:@"setOwnChangeSuccess" object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(refreshData) name:@"addUserSuccess" object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(getUserList:) name:NOTIFICATION_RESPONSE_GET_DEVICE_USERS_LIST object:nil];

    _permissionsView = [[SampuxPermissionsView alloc]initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height)];
    [self.view addSubview:_permissionsView];
    _permissionsView.model = self.model;
    self.permissionsView = _permissionsView;
    [self createBackBarButtonItem];
    [self createNavBarButtonItem];
    _permissionsView.rowDelegate = self;
}

- (void)createNavBarButtonItem
{
    _authorityTransferBtn = [UIButton createButtonWithImageName:@"more" frame:CGRectMake(self.view.width - 30 - 5 , 5, 30, 30)];
    [self.view addSubview:_authorityTransferBtn];
    _authorityTransferBtn.tag = THAuthorityTransferBtnType;
    [self.navigationController.navigationBar addSubview:_authorityTransferBtn];
    _authorityTransferBtn.hitTestEdgeInsets = UIEdgeInsetsMake(-10, -10, -10, 0);
    
    _addUserBtn = [UIButton createButtonWithImageName:@"add" frame:CGRectMake(_authorityTransferBtn.left - 5 - 30, 5, 30, 30)];
    _addUserBtn.tag = THAddUserBtnType;
    [self.view addSubview:_addUserBtn];
    [self.navigationController.navigationBar addSubview:_addUserBtn];
    _addUserBtn.hitTestEdgeInsets = UIEdgeInsetsMake(-10, 0, -10, -10);

    [_authorityTransferBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [_addUserBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
}

- (SampuxAuthorityTransferView *)transferView
{
    if (!_transferView) {
        _transferView = [[SampuxAuthorityTransferView alloc]initWithFrame:[UIScreen mainScreen].bounds];
        __weak typeof(SampuxInstallPermissionsVC *) wSelf = self;
        _transferView.model = self.model;
        _transferView.transferBlock = ^{
            dispatch_async(dispatch_get_main_queue(), ^{
                [wSelf.navigationController popToRootViewControllerAnimated:YES];
            });
        };
    }
    return _transferView;
}

- (SampuxAddUserView *)addUserView
{
    if (!_addUserView) {
        _addUserView = [[SampuxAddUserView alloc]initWithFrame:[UIScreen mainScreen].bounds];
        _addUserView.addUserDelegate = self;
        _addUserView.model = self.model;
    }
    return _addUserView;
}

- (SampuxBubbleView *)bubbleView
{
    if (!_bubbleView) {
        _bubbleView = [[SampuxBubbleView alloc]initWithFrame:[UIScreen mainScreen].bounds];
        __weak typeof(SampuxInstallPermissionsVC *) wSelf = self;
        _bubbleView.backBlock = ^()
        {
            [wSelf.navigationController popToRootViewControllerAnimated:YES];
        };
        _bubbleView.addBlock = ^()
        {
            AppDelegate *delegate = [UIApplication sharedApplication].delegate;
            delegate.window.backgroundColor = THSystemBlackColor;
            [delegate.window addSubview:wSelf.transferView];
        };
    }
    return _bubbleView;
}

- (void)setModel:(SPDeviceConfigModel *)model
{
    _model = model;
}

- (void)refreshData
{
    self.needRefresh = YES;
}

- (void)btnClick:(UIButton *)btn
{
    if (btn.tag == THAuthorityTransferBtnType) {
        [self addViewToWindow:self.bubbleView];
    } else {
        [self addViewToWindow:self.addUserView];
    }
}

- (void)addViewToWindow:(id)subView
{
    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    delegate.window.backgroundColor = THSystemBlackColor;
    [delegate.window addSubview:subView];
}

- (void)addUserViewYesBtnClick:(NSMutableArray *)userArr
{
    dispatch_async(dispatch_get_main_queue(), ^{
        self.needRefresh = YES;
    });
//    self.permissionsView.dataSourceArr
//    [self.permissionsView.dataSourceArr addObjectsFromArray:userArr];
//    self.permissionsView.dataSourceArr  = self.permissionsView.dataSourceArr ;
}

- (void)seletedHailFellowRow:(NSIndexPath *)index
{
    _indexPath = index;
    GBInterMaster_Bean_DeviceOperatorInfo *info = self.permissionsView.dataSourceArr[index.row];
    SampuxRecordViewController *recordVC = [[SampuxRecordViewController alloc]init];
    recordVC.userCount = info.userAccount;
    recordVC.model = self.model;
    [self.navigationController pushViewController:recordVC animated:YES];
}

- (void)viewDidDisappear:(BOOL)animated
{
    UITableViewCell *cell = [self.permissionsView.firendTableView cellForRowAtIndexPath:_indexPath];
    cell.backgroundColor = THWhiteAlplaColor(0.2);
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:YES];
    _authorityTransferBtn.hidden = YES;
    _addUserBtn.hidden = YES;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    UITableViewCell *cell = [_permissionsView.firendTableView cellForRowAtIndexPath:_indexPath];
    cell.backgroundColor = THWhiteAlplaColor(0.1);
    _authorityTransferBtn.hidden = NO;
    _addUserBtn.hidden = NO;
    SampuxCustomNavController *nav = (SampuxCustomNavController *)self.navigationController;
    nav.titleStr = @"权限设置";
    nav.rightBtn.hidden = YES;
    self.needRefresh = YES;
}

- (void)setNeedRefresh:(BOOL)needRefresh
{
    _needRefresh = needRefresh;
     if (_needRefresh) {
        [_permissionsView.header beginRefreshing];
    }
}

- (void)getUserList:(NSNotification *)notification
{
    id userInfo = [notification userInfo];
    if(userInfo == nil){
        return;
    }
    NSDictionary * result = userInfo;
    int msgNumber = [[result objectForKey:@"msgNumber"] intValue];
    int responseCode = [[result objectForKey:@"responseCode"] intValue];
    NSLog(@"getUserList == 结果，消息序号：%d，响应码：%x",msgNumber,responseCode);
    if(responseCode == 0x00){
        NSLog(@"%@",[result objectForKey:@"operators"]);
        NSMutableArray *userListArr = [result objectForKey:@"operators"];
        self.permissionsView.dataSourceArr = userListArr;
    }else
    {
        [SVProgressHUD showErrorWithStatus:@"获取使用者列表失败"];
//        [MBProgressHUD showError:@"获取使用者列表失败"];
    }
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
@end
